package org.liang.activemq.service.impl;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.liang.activemq.service.MqSentService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

/**
 * 
 * activeMq消息测试发送端
 * @author liang
 * @date 2018年1月25日 下午2:17:06
 * @version 1.0
 */
@Service
public class MqSentServiceImpl implements MqSentService{

	@Qualifier("jmsQueueTemplate")//Queue类型,队列消息
	private JmsTemplate jmsQueueTemplate;
	
	@Qualifier("jmsTopicTemplate")//Topic类型,订阅消息
	private JmsTemplate jmsTopicTemplate;
	
	@Override
	public void sendMsg(String msg) {
		System.out.println("mq开始发送消息,Queue类型,队列消息：" + msg);
		String tempMsg = "你发送的消息是：" + msg;
		jmsQueueTemplate.send(msg, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(tempMsg);
            }
        });
	}
	
	public void senMsgTopic(String msg) {
		System.out.println("mq开始发送消息,Topic类型,订阅消息：" + msg);
		String tempMsg = "你发送的消息是：" + msg;
		
		Destination destination = jmsQueueTemplate.getDefaultDestination();
		jmsQueueTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(tempMsg);
            }
        });
	}
}
